﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class OperationLog: IOperationLog
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public OperationLog(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.OperationLog GetOperationLog(System.Int32 operationLogID)
		{
			BEGDC.PreContract.DAL.OperationLog operationLog = BEGDC.PreContract.DAL.OperationLog.TryFind(operationLogID);

            return operationLog;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetOperationLog_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM OperationLog";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteOperationLog(System.Int32 operationLogID)
		{
			try
            {
				BEGDC.PreContract.DAL.OperationLog operationLog = BEGDC.PreContract.DAL.OperationLog.TryFind(operationLogID);
                operationLog.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("OperationLog", string.Format("Error occured when deleting the OperationLog, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddOperationLog(System.String employeeName, System.String operationType, System.String logContent, Nullables.NullableDateTime operationDate, System.String hostName, System.String hostIP)	
		{
			try
            {
				BEGDC.PreContract.DAL.OperationLog operationLog = new BEGDC.PreContract.DAL.OperationLog();
				
				operationLog.EmployeeName = employeeName;
				operationLog.OperationType = operationType;
				operationLog.LogContent = logContent;
				operationLog.OperationDate = operationDate;
				operationLog.HostName = hostName;
				operationLog.HostIP = hostIP;
				operationLog.CreateAndFlush();
				
				return operationLog.OperationLogID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("OperationLog", string.Format("Error occured when adding a new OperationLog, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateOperationLog(System.Int32 operationLogID, System.String employeeName, System.String operationType, System.String logContent, Nullables.NullableDateTime operationDate, System.String hostName, System.String hostIP)
		{
			try
            {
                BEGDC.PreContract.DAL.OperationLog operationLog = GetOperationLog(operationLogID);

				operationLog.EmployeeName = employeeName;
				operationLog.OperationType = operationType;
				operationLog.LogContent = logContent;
				operationLog.OperationDate = operationDate;
				operationLog.HostName = hostName;
				operationLog.HostIP = hostIP;
				operationLog.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("OperationLog", string.Format("Error occured when updating the OperationLog, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



